const { create, queryUserInfoByid, updateUserInfoByToken, updateUserInfoBywxOpenid } = require("../server/userServer.js");
const jwt = require("jsonwebtoken");
const { PRIVATE_KEY, PUBLIC_KEY } = require("../config/secret.js");


class Controller {
	// 游客登录
	async create(ctx, next) {
		const data = await create();
		ctx.body = {
			code:0 ,
			message:'初始化成功',
			data
		}
	}

	// 根据id获取用户信息
	async getUserInfo(ctx , next){
		const {user_id} = ctx.request.body
		const data = await queryUserInfoByid(user_id)
		ctx.body = {
			code:0 ,
			message:'获取用户信息成功',
			data
		}
	}

	async signToken(ctx , next){
		const { user_id , username , password } = ctx.request.body
		console.log("user_id , username , password" , user_id , username , password);
		const token = jwt.sign({ user_id }, PRIVATE_KEY, {
			expiresIn: 24 * 60 * 60 * 365, // 过期时间 一天24小时  * 365 一年
			algorithm: "RS256",
		});
		const data = await updateUserInfoByToken(user_id ,username, password , token)
		ctx.body = {
			code:0 ,
			message:"绑定成功",
			data
		}
	}

	async updateUserInfo(ctx,next){
		const {user_id} = ctx.request.body
		const wx_openid = ctx.wx_openid
		const data = await updateUserInfoBywxOpenid(wx_openid,user_id)
		ctx.body = {
			code:0 ,
			message:"微信登录成功~",
			data
		}
	}
	
}

module.exports = new Controller();
